$(function() {
	layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function(){
		 var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  ,layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,carousel = layui.carousel //轮播
		  ,upload = layui.upload //上传
		  ,element = layui.element //元素操作
		  ,slider = layui.slider //滑块
	var T =	table.render({
	    elem: '#btachcourseware'
	    ,height: 420
	    ,url: '/selectBatchCourseWare' //数据接口
	    ,title: '批次-课件表'
	    ,page: true //开启分页
	    ,limit:5
	    ,limits:[5,10,15,20]
	    ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
	    ,totalRow: true //开启合计行
	    ,response : {
            statusCode : 200// 重新规定成功的状态码为 200，table 组件默认为 0
        }
	    ,parseData : function(res) {
	    	console.log(res);// res 即为原始返回的数据
            return {
                "count" : res.tol,
                "code" : res.status, // 解析接口状态
                "msg" : res.message, // 解析提示文本
                "data" : res.list
                // 解析数据列表
            };
        }
	    ,cols: [[ //表头
	      {type: 'checkbox', fixed: 'left'}
	      ,{field: 'batch_courseware_id', title: 'ID', width:80,align:'center', sort: true, fixed: 'left'}
	      ,{field: 'batchId', title: '批次ID',align:'center', width:150}
	      ,{field: 'batchName', title: '批次名字',align:'center', width: 200}
	      ,{field: 'coursewareId', title: '课件ID',align:'center', width:150}
	      ,{field: 'coursewareName', title: '课件名字',align:'center', width: 200,}
	      ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo4'}
	    ]]
	  });
		 
		 table.on('tool(btachcourseware)', function(obj){
			 var data=obj.data;
			 console.log(data);
			var batch_courseware_id = data.batch_courseware_id;
			 if(obj.event=='del'){
				 layer.confirm('真的删除行吗',function(index) {
					 $.ajax({
						 url:"/deleteBatchCourseWare",
	             		dataType: 'json',
	                     type: 'post',
	                     data:{batch_courseware_id:batch_courseware_id},
	                     success: function () {
	                    	 T.reload();
	                    	 obj.del();
							layer.close(index);
	                     	layer.msg("删除成功");
	                     },
	                     error: function () {
	                        layer.msg("删除失败");
	                     }
					 })
					 })
			 }else if(obj.event=='update'){
				 layer.open({
						type : 1,
						skin : 'layui-layer-rim', // 加上边框
						area : [ '520px', '400px' ], // 宽高
						content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
								+  '  <br>\n'
								
								+  '  <div class="layui-form-item">\n' 
			                    +  '    <label class="layui-form-label">批次名字</label>\n' 
			                    +  '    <div class="layui-input-block">\n'
			                    +  '    <select id="batchName"></select>\n' 
			                    +  '  </div>\n'
			                    +  '  </div>\n'
								
								+  '  <div class="layui-form-item">\n' 
			                    +  '    <label class="layui-form-label">课件名字</label>\n' 
			                    +  '    <div class="layui-input-block">\n'
			                    +  '    <select id="coursewareName"></select>\n' 
			                    +  '  </div>\n'
			                    +  '  </div>\n'
								
								+  '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
								+  '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
								+  '</form>'
					});
				 
				 $.ajax({
			    		url:"/selectCourseWares",
			    		dataType: 'json',
			            type: 'post',
			            success: function (data) {
			            	var da = eval(data);
			            	console.log(da);
			            	$("#coursewareName").append("<option ></option>");
			            	for(var i=0;i<da.length;i++){
			            		$("#coursewareName").append("<option value=\""+da[i].coursewareId+"\">"+da[i].coursewareName+"</option>");
			            		var form = layui.form;
			            		form.render('select');
			            	}
			            	
			            },
			            error: function () {
			                layer.alert("读取失败");
			            }
			        })
			        
			        $.ajax({
			    		url:"/selectBatchs",
			    		dataType: 'json',
			            type: 'post',
			            success: function (data) {
			            	var da = eval(data);
			            	console.log(da);
			            	$("#batchName").append("<option ></option>");
			            	for(var i=0;i<da.length;i++){
			            		$("#batchName").append("<option value=\""+da[i].batchId+"\">"+da[i].batchName+"</option>");
			            		var form = layui.form;
			            		form.render('select');
			            	}
			            	
			            },
			            error: function () {
			                layer.alert("读取失败");
			            }
			        })
			        
			        
			     
				 	 $("body").on("click","#btn12",function(){
				 		   var batchId=$("#batchName").val();
						 	var coursewareId=$("#coursewareName").val();
						 if(batchId!=null && coursewareId!=null && coursewareId!='' && batchId!=''){
							 $.ajax({
						    		url:"/updateBatchCourseWare",
						    		dataType: 'json',
						            type: 'post',
						            data:{batchId:batchId,coursewareId:coursewareId,batch_courseware_id:data.batch_courseware_id},
						            success: function (res) {
						            	if(res.message=="yes"){
						            	layer.closeAll();
						            	T.reload();
						            	layer.msg("修改成功");
						            	}else{
						            		layer.msg("存在相同批次的相同课件");
						            	}
						           
						            },
						            error: function () {
						                layer.msg("修改失败");
						            }
						        })
							 }else{
								 layer.msg("存在空值，请补齐提交");
							 }
						 })
				 	 }
			 
		 })	 
		
		 
		 $("#add3").on("click",function(){
			 layer.open({
						type : 1,
						skin : 'layui-layer-rim', // 加上边框
						area : [ '520px', '400px' ], // 宽高
						content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
								+  '  <br>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">ID</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <input type="text"  id="batch_courseware_id" lay-verify="title" autocomplete="off" class="layui-input">\n'
								+ '    </div>\n'
								+ '  </div>\n'
								+  '  <div class="layui-form-item">\n' 
			                    +  '    <label class="layui-form-label">批次名字</label>\n' 
			                    +  '    <div class="layui-input-block">\n'
			                    +  '    <select id="batchName"></select>\n' 
			                    +  '  </div>\n'
			                    +  '  </div>\n'
								
								+  '  <div class="layui-form-item">\n' 
			                    +  '    <label class="layui-form-label">课件名字</label>\n' 
			                    +  '    <div class="layui-input-block">\n'
			                    +  '    <select id="coursewareName"></select>\n' 
			                    +  '  </div>\n'
			                    +  '  </div>\n'
								
								+  '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
								+  '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
								+  '</form>'
					});
			 $.ajax({
		    		url:"/selectCourseWares",
		    		dataType: 'json',
		            type: 'post',
		            success: function (data) {
		            	var da = eval(data);
		            	console.log(da);
		            	$("#coursewareName").append("<option ></option>");
		            	for(var i=0;i<da.length;i++){
		            		$("#coursewareName").append("<option value=\""+da[i].coursewareId+"\">"+da[i].coursewareName+"</option>");
		            		var form = layui.form;
		            		form.render('select');
		            	}
		            	
		            },
		            error: function () {
		                layer.alert("读取失败");
		            }
		        })
		        
		        $.ajax({
		    		url:"/selectBatchs",
		    		dataType: 'json',
		            type: 'post',
		            success: function (data) {
		            	var da = eval(data);
		            	console.log(da);
		            	$("#batchName").append("<option ></option>");
		            	for(var i=0;i<da.length;i++){
		            		$("#batchName").append("<option value=\""+da[i].batchId+"\">"+da[i].batchName+"</option>");
		            		var form = layui.form;
		            		form.render('select');
		            	}
		            	
		            },
		            error: function () {
		                layer.alert("读取失败");
		            }
		        })
		         $("body").on("click","#btn12",function(){
				 		   var batchId=$("#batchName").val();
						 	var coursewareId=$("#coursewareName").val();
						 	var batch_courseware_id=$("#batch_courseware_id").val();
						 if(batchId!=null && coursewareId!=null && coursewareId!='' && batchId!='' && batch_courseware_id!=null && batch_courseware_id!=''){
							 $.ajax({
						    		url:"/insertBatchCourseWare",
						    		dataType: 'json',
						            type: 'post',
						            data:{batchId:batchId,coursewareId:coursewareId,batch_courseware_id:batch_courseware_id},
						            success: function (res) {
						            	if(res.message=="yes"){
						            	layer.closeAll();
						            	T.reload();
						            	layer.msg("插入成功");
						            	}else{
						            		layer.msg("ID相同或存在相同批次的相同课件");
						            	}
						           
						            },
						            error: function () {
						                layer.msg("插入失败");
						            }
						        })
							 }else{
								 layer.msg("存在空值，请补齐提交");
							 }
						 })
		 })
	})
})